<?php

namespace app\dao\office;

use app\dao\BaseDao;
use app\model\office\OfficeChapter;
use app\model\office\OfficeStudyRecord;

class OfficeChapterDao extends BaseDao
{

    public function getList(array $where)
    {
        return $this ->getModel() -> where('pid','<>',0) ->  where('website_id',$where['website_id']) ->where('class_id',$where['class_id']) ->field('title,id') -> paginate($where['pageSize']);
    }

    public function index(array $where)
    {
        return $this ->search() ->with(['OfficeClass' => function($query){
            $query->withField(['id', 'title']);
        }])  ->withSearch(['class_id'],['class_id' => $where['class_id']]) -> order('class_id','asc') ->  where('website_id',$where['website_id']) ->paginate($where['pageSize']);
    }

    public function updateDao(array $where)
    {
        unset($where['website_id']);
        return $this ->getModel() ->update($where);
    }

    public function create(array $where)
    {
        return $this -> getModel() ->create($where);
    }

    public function deleteDao(array $where)
    {
        return $this->getModel()-> whereIn('id',$where['id']) -> update(['delete_time' => date('Y-m-d H:i:s', time())]);

    }

    public function getPidList(array $where)
    {
        return $this ->getModel() -> where('class_id',$where['class_id'])  -> where('pid',0) -> where('website_id',$where['website_id']) -> select();
    }

    protected function setModel(): string
    {
        return  OfficeChapter::class;
    }

    //--------------------------------------api------------------------------------------------------------------------

    /*
     * 获取章节目录
     */
    public function getChapter(array $where){

        return  $this->getModel()
            ->withoutField(['weight','create_time','update_time','delete_time','website_id'])
            ->order('weight','asc')
            ->where('status',1)
            ->where('class_id',$where['id'])
            ->select();

    }

    public function getVideoRecord(array $where){
        return OfficeStudyRecord::where('website_id',$where['website_id'])
            ->where('class_id',$where['id'])
            ->where('user_id',$where['user_id'])
            ->field(['id','chapter_name','is_pass','play_time','chapter_id'])
            ->select();
    }

}